#!/bin/bash
echo "更新软件包中..."
apt update && apt upgrade -y
echo "更新完成"
if grep -Eqi "Ubuntu" /etc/issue; then
if grep -Eq "Ubuntu" /etc/*-release; then
    echo "系统为ubuntu"
    else
        "请在Ubuntu环境运行此脚本哦"
        exit 1
fi
fi
if [ "$(whoami)" != "root" ]; then
    sudo -i
fi

if ! [ -x "$(command -v wget)" ]; then
    apt install wget -y
fi

if ! [ -x "$(command -v npm)" ]; then
    echo "正在通过云溪院获取node"
    sleep 1
    wget -c https://gitee.com/yunxiyuan/yxybot/releases/download/linux/node-v18.15.0-linux-arm64.tar.xz
    if ! [ -x "$(command -v tar)" ]; then
        apt install tar -y
    fi
    apt-get install xz-utils
    xz -d node-v18.15.0-linux-arm64.tar.xz
    tar xvf node-v18.15.0-linux-arm64.tar
    mv node-v18.15.0-linux-arm64 /opt/
    ln -d /opt/node-v18.15.0-linux-arm64/bin/node /usr/local/bin/node
    ln -s /opt/node-v18.15.0-linux-arm64/bin/npm /usr/local/bin/npm
    rm -rf /root/node-v18.15.0-linux-arm64.tar
    echo "node 安装成功"
elif ! [ -x "$(command -v npm)" ]; then
    wget -c https://nodejs.org/dist/v18.13.0/node-v18.13.0-linux-x64.tar.xz
    tar -xvf node-v18.13.0-linux-x64.tar.xz
    if [ -f "/usr/bin/node" ]; then
        rm -rf /usr/bin/node
    fi
    if [ -f "/usr/bin/npm" ]; then
        rm -rf /usr/bin/npm
    fi
    ln -s /root/node-v18.13.0-linux-x64/bin/node /usr/bin/node
    ln -s /root/node-v18.13.0-linux-x64/bin/npm /usr/bin/npm
    cd ~
elif ! [ -x "$(command -v npm)" ]; then
    curl -sL https://deb.nodesource.com/setup_18.x | bash  -
    apt-get install -y nodejs
fi

if ! [ -x "$(command -v pnpm)" ]; then
    wget -c https://gitee.com/yunxiyuan/yxybot/releases/download/linux/pnpm-linux-arm64
    mv pnpm-linux-arm64 /usr/local/bin/pnpm
    sleep 3
    if ! [ -f "/usr/local/bin/pnpm" ]; then
        mv pnpm-linux-arm64 /usr/local/bin/pnpm
    fi
    chmod 777 /usr/local/bin/pnpm
    ls -l /usr/local/bin/pnpm
elif ! [ -x "$(command -v pnpm)" ]; then
    npm install -g pnpm --registry=https://registry.npm.taobao.org
    cd ~
fi

if command -v chromium-browser >/dev/null 2>&1; then
  echo "已下载chromium"
else
  echo "正在下载 chromium"
  git clone https://gitee.com/Ganyu256/chromium.git --depth=1
fi

function download_and_install {
    local package_name=$1
    local download_command=$2
    local install_command=$3

    if ! [ -x "$(command -v $package_name)" ]; then
        echo "正在下载 $package_name"
        eval $download_command
        echo "$package_name 下载成功"

        echo "正在安装 $package_name"
        eval $install_command
        echo "$package_name 安装成功"
    fi
}

download_and_install "redis" "apt install -y redis" "redis-server --daemonize yes --save 900 1 --save 300 10"
download_and_install "git" "apt install -y git" ""
download_and_install "fonts-wqy-microhei" "apt install -y --force-yes --no-install-recommends fonts-wqy-microhei" ""
download_and_install "ttf-wqy-zenhei" "apt install -y --force-yes --no-install-recommends ttf-wqy-zenhei" ""

if [ -d "/root/Miao-Yunzai" ] || [ -d "/root/Yunzai-Bot" ] || [ -d "/root/yxybot" ]; then
    echo "你已安装云崽，正在退出"
    exit 1
fi

echo "----------安装云崽---------"
echo "    1.安装 Miao-Yunzai "
echo "    2.安装 Yunzai-Bot  "
echo "    3.安装 yxybot      "
echo "---------------------------"
read -p "请选择你要安装的云崽: " var

case $var in
    1)
        echo "开始安装 Miao-Yunzai"
        git clone --depth=1 https://gitee.com/yoimiya-kokomi/Miao-Yunzai.git
        cd Miao-Yunzai
        git clone --depth=1 https://gitee.com/yoimiya-kokomi/miao-plugin.git ./plugins/miao-plugin/
        pnpm install image-size -w
        pnpm install -P
        pnpm i
        cd $HOME
        ;;
    2)
        echo "开始安装 Yunzai-Bot"
        git clone --depth=1 -b main https://gitee.com/yoimiya-kokomi/Yunzai-Bot.git
        cd Yunzai-Bot  
        echo "开始安装 喵喵插件"  
        git clone --depth=1 https://gitee.com/yoimiya-kokomi/miao-plugin.git ./plugins/miao-plugin/       
        pnpm install -P
        cd $HOME
        ;;
    3) 
        echo "开始安装 云溪院月崽 "
        git clone --depth=1 -b v1 https://gitee.com/yunxiyuan/yxybot
        pnpm install -P
        pnpm i
        cd $HOME        
        ;;
    *)
        echo "由于笨比的你乱选，开始自动下载miao-yunzai"
        git clone --depth=1 https://gitee.com/yoimiya-kokomi/Miao-Yunzai.git
        cd Miao-Yunzai
        git clone --depth=1 https://gitee.com/yoimiya-kokomi/miao-plugin.git ./plugins/miao-plugin/
        pnpm install image-size -w
        pnpm install -P
        pnpm i
        cd $HOME
        ;;
esac

if command -v chromium-browser >/dev/null 2>&1; then
  echo "嘎嘎嘎嘎嘎"
else
  echo "开始解码chrome与云崽的适配"
  git clone https://gitee.com/Ganyu256/chromium.git --depth=1
  cd chromium
  bash install.sh
  echo "chromium 配置成功"
  cd $HOME
fi

if [ -d "$HOME/Miao-Yunzai" ]; then
    lsy="$HOME/Miao-Yunzai"
elif [ -d "$HOME/Yunzai-Bot" ]; then
    lsy="$HOME/Yunzai-Bot"
elif [ -d "$HOME/yxybot" ]; then
    lsy="$HOME/yxybot"
else
    echo "您未安装机器人"
    exit 1
fi

cd $HOME

xrk="$HOME/xrk"
jh="$lsy/plugins"

if [ ! -d "$jh/xiaoyue-plugin" ]; then
    cd $lsy
    git clone --depth=1 https://gitee.com/yunxiyuan/xiaoyue-plugin.git ./plugins/xiaoyue-plugin
    cd ~
fi

if ! grep -q "cat .ml" "$HOME/.profile"; then
    sed -i '/cat .yzml/d' "$HOME/.profile"
    sed -i '9a cat .ml' "$HOME/.profile"
    sed -i '10!s/cat .ml//' "$HOME/.profile"
fi

yz="/usr/local/bin/yz"
if [ -f "$yz" ]; then
    rm /usr/bin/yz
fi
echo '#!/bin/bash' > "$yz"
echo 'echo 正在启动Yunzai-Bot' >> "$yz"
echo "cd $lsy" >> "$yz"
echo 'redis-server --daemonize yes --save 900 1 --save 300 10' >> "$yz"
echo 'node app' >> "$yz"
chmod +x "$yz"

yzlog="/usr/local/bin/yzlog"
if [ -f "$yzlog" ]; then
    rm /usr/bin/yzlog
fi
echo '#!/bin/bash' > "$yzlog"
echo 'echo 正在打开Yunzai-Bot后台日志' >> "$yzlog"
echo "cd $lsy" >> "$yzlog"
echo 'pnpm run log' >> "$yzlog"
chmod +x "$yzlog"
    
yzlogin="/usr/local/bin/yzlogin"
if [ -f "$yzlogin" ]; then
    rm /usr/bin/yzlogin
fi
echo '#!/bin/bash' > "$yzlogin"
echo 'echo 正在打开Yunzai-Bot登录配置' >> "$yzlogin"
echo "cd $lsy" >> "$yzlogin"
echo 'redis-server --daemonize yes --save 900 1 --save 300 10' >> "$yzlogin"
echo 'pnpm run login' >> "$yzlogin"
chmod +x "$yzlogin"

yzstop="/usr/local/bin/yzstop"
if [ -f "$yzstop" ]; then
    rm -rf /usr/bin/yzstop
fi
echo '#!/bin/bash' > "$yzstop"
echo 'echo 正在停止Yunzai-Bot运行' >> "$yzstop"
echo "cd $lsy" >> "$yzstop"
echo 'pnpm stop' >> "$yzstop"
chmod +x "$yzstop"

xrk="/usr/local/bin/xrk"
if [ -f "$xrk" ]; then
    rm -rf /usr/bin/xrk
fi
bash <(curl -sL https://gitee.com/xrk114514/shell-Yunzai/raw/master/xr.sh)
xrrr=".ml"
iuyb=(
    "笨比启动云崽命令为 yz"
    "后台查看日志命令为 yzlog"
    "重新配置账号命令为 yzlogin"
    "停止云崽运行命令为 yzstop"
    "启动向日葵脚本命令为 xrk"
)
    
if [ -f "$xrrr" ]; then
    rm $HOME/.ml
fi

if [ ! -f "$xrrr" ]; then
    for cmd in "${iuyb[@]}" ; do
        key=${cmd% *}
        value=${cmd##* }
        echo "$key $value" >> "$xrrr"
    done
fi

echo "启动命令写入成功"